import { devDebug } from '../../../utils/settings';
import { coupon_state_map, sport_map } from '../../../utils/dataset/db_data'

Page({
  data: {
    orderId: '',
    couponList: [],
    selectedIdx: [],
    selectedAll: false,
    deadTimeStr: '',
    lockSubmit: 0,
    timeSider: [],
  },
  onLoad(options) {
    this.setData({ orderId: options.order_id })
    this.initTime();
    this.loadCoupon();
  },
  async loadCoupon() {
    let resList = await new Promise(resolve => {
      wx.cloud.callFunction({
        name: 'db_coupon',
        data: {
          funcName: 'query',
          searchData: { order_id: this.data.orderId },
        },
        success: (r) => resolve(r.result),
      });
    })
    resList.sort((a, b) => {
      if (a.coupon_state == b.coupon_state) return a.end_time - b.end_time;
      return a.coupon_state - b.coupon_state
    })
    let deadTime = 0;
    const timeSider = this.data.timeSider;
    resList = resList.map(o => {
      deadTime = Math.max(o.end_time);
      o.start_time = new Date(o.start_time).Format()
      o.end_time = new Date(o.end_time).Format()
      o.coupon_state_text = coupon_state_map.get(o.coupon_state)
      o.time_quantum = [timeSider[o.sku.rid], timeSider[o.sku.rid + 1]];
      o.court_name = o.court[0].court_name;
      return o;
    })
    this.setData({ couponList: resList, deadTimeStr: new Date(deadTime).Format() })
    console.log(this.data.couponList);
  },
  onSelect(ev) {
    this.setData({ selectedIdx: ev.detail })
    if (ev.detail.length == this.data.couponList.reduce((tot, cur) => (tot + (cur.coupon_state == 0 ? 1 : 0)), 0)) {
      this.setData({ selectedAll: true })
    } else {
      this.setData({ selectedAll: false })
    }
  },
  onSelectAll(ev) {
    this.setData({ selectedAll: ev.detail })
    if (ev.detail) {
      const arr = [];
      Array.apply(null, { length: this.data.couponList.length }).forEach((v, i) => {
        if (this.data.couponList[i].coupon_state == 0) arr.push('' + i);
      })
      this.setData({ selectedIdx: arr })
    } else {
      this.setData({ selectedIdx: [] })
    }
  },
  async onConfirm() {
    this.setData({ lockSubmit: 1 });
    let coupon_ids = [];
    this.data.selectedIdx.forEach(i => coupon_ids.push(this.data.couponList[i]._id))
    await new Promise(resolve => {
      wx.cloud.callFunction({
        name: 'db_coupon',
        data: {
          funcName: 'verify',
          order_id: this.data.orderId,
          coupon_ids: coupon_ids,
          remark: '卡券已核销'
        },
        success: (r) => resolve(r.result),
      });
    })
    this.setData({ lockSubmit: 0 })
    this.setData({ selectedIdx: [], selectedAll: false, deadTimeStr: '' })
    this.loadCoupon()
  },
  initTime() {
    const hh = Array.apply(null, { length: 25 }).map((o, i) => {
      let s = '00' + i;
      return s.substring(s.length - 2);
    });
    const mm = ['00', '30'];
    const timeSider = [];
    for (let i = 0; i <= 48; i++) {
      timeSider.push(hh[i >> 1] + ':' + mm[i & 1]);
    }
    this.setData({ timeSider })
  }
})